<html>
<head><meta charset="utf-8"><title>representation of references · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html">representation of references</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="148794746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794746">(Nov 29 2018 at 15:38)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span>  I don't understand the latest comments in the representation of references</p>



<a name="148794798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794798">(Nov 29 2018 at 15:39)</a>:</h4>
<p>Alignment is part of representation, but whether Option::&lt;&amp;T&gt;::None is "equal" to ptr::null::&lt;*const T&gt;() is part of validity</p>



<a name="148794802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794802">(Nov 29 2018 at 15:39)</a>:</h4>
<p>You mean <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/16#issuecomment-442877094" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/16#issuecomment-442877094">https://github.com/rust-rfcs/unsafe-code-guidelines/issues/16#issuecomment-442877094</a>?</p>



<a name="148794823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794823">(Nov 29 2018 at 15:39)</a>:</h4>
<p>I don't know if there is any document defining these terms yet</p>



<a name="148794899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794899">(Nov 29 2018 at 15:40)</a>:</h4>
<p>I'm unsure that I understood them correctly</p>



<a name="148794924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794924">(Nov 29 2018 at 15:40)</a>:</h4>
<p>Indeed, we're hitting issues about basic definitions :/</p>



<a name="148794931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794931">(Nov 29 2018 at 15:40)</a>:</h4>
<p>AFAIK there are at least three terms we use here: representation, validity, and safety</p>



<a name="148794969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794969">(Nov 29 2018 at 15:41)</a>:</h4>
<p>representation answer the question: what shape do the bits that the type occupies in memory have</p>



<a name="148794973"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794973" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794973">(Nov 29 2018 at 15:41)</a>:</h4>
<p>Certainly being non-zero is part of representation, but I'm not sure about alignment.</p>



<a name="148794987"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794987" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794987">(Nov 29 2018 at 15:41)</a>:</h4>
<p>aligned of the type is part of representation, but whether the <em>value</em> of type <code>&amp;T</code> must be aligned is part of validity</p>



<a name="148794992"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148794992" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148794992">(Nov 29 2018 at 15:41)</a>:</h4>
<p>that's basically, what's the type's size, what's its alignment</p>



<a name="148795010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795010">(Nov 29 2018 at 15:41)</a>:</h4>
<blockquote>
<p>Certainly being non-zero is part of representation, but I'm not sure about alignment.</p>
</blockquote>
<p>no, non-zero is also validity</p>



<a name="148795061"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795061" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795061">(Nov 29 2018 at 15:42)</a>:</h4>
<p>repr is: align+size of the type, and then the offsets of its fields. and ABI stuff.</p>



<a name="148795063"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795063" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795063">(Nov 29 2018 at 15:42)</a>:</h4>
<p>for DSTs representation is more complicated, and attributes like repr(C, packed, align, simd, ...) alter all of this</p>



<a name="148795074"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795074" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795074">(Nov 29 2018 at 15:42)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> but non-zero impacts repr, due to <code>Option&lt;T&gt;</code> optimization.</p>



<a name="148795075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795075">(Nov 29 2018 at 15:42)</a>:</h4>
<p>ah yes, ABI / calling convention stuff is probably also part of representation</p>



<a name="148795093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795093">(Nov 29 2018 at 15:42)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> no it does not, that impacts validity</p>



<a name="148795096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795096">(Nov 29 2018 at 15:42)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> enums exploit the validity invariant in how their layout is computed, yes</p>



<a name="148795105"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795105" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795105">(Nov 29 2018 at 15:42)</a>:</h4>
<p>the discussions are not entirely separate</p>



<a name="148795126"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795126" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795126">(Nov 29 2018 at 15:43)</a>:</h4>
<p>The size, alignment, of Option&lt;&amp;T&gt;  does not depend on which value ptr::null has</p>



<a name="148795131"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795131" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795131">(Nov 29 2018 at 15:43)</a>:</h4>
<p>but there was so much to talk about in terms of field offsets for structs and tranmuting arrays to homogenous tuples and such things that we decided to split them up</p>



<a name="148795133"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795133" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795133">(Nov 29 2018 at 15:43)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> so for you, repr can be based on validity?</p>



<a name="148795146"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795146" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795146">(Nov 29 2018 at 15:43)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> it's a cyclic dependency</p>



<a name="148795147"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795147" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795147">(Nov 29 2018 at 15:43)</a>:</h4>
<p>validity: which values are the bits of the representation allowed to take</p>



<a name="148795150"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795150" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795150">(Nov 29 2018 at 15:43)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> ugh</p>



<a name="148795156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795156">(Nov 29 2018 at 15:43)</a>:</h4>
<p>validity of a struct depends on repr, i.e., on the offsets of the fields</p>



<a name="148795161"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795161" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795161">(Nov 29 2018 at 15:43)</a>:</h4>
<p>I thought validity depended on repr, but not vice versa.</p>



<a name="148795210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795210">(Nov 29 2018 at 15:44)</a>:</h4>
<p>repr: what's the shape of the bits, validity: which values can the bits take</p>



<a name="148795215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795215">(Nov 29 2018 at 15:44)</a>:</h4>
<p>yeah no enum optimizations make these form a cycle</p>



<a name="148795218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795218">(Nov 29 2018 at 15:44)</a>:</h4>
<p>depending on padding, etc. , repr interacts with validity</p>



<a name="148795230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795230">(Nov 29 2018 at 15:44)</a>:</h4>
<p>but most of the time one can treat these separate</p>



<a name="148795293"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795293" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795293">(Nov 29 2018 at 15:45)</a>:</h4>
<p>so if we say: the bit values of <code>ptr::null()</code> are not a <strong>valid</strong> bit pattern for <code>&amp;T</code>, that's validity, and because <code>Option</code> is an enum, and enums can exploit invalid bit patterns (called <strong>niches</strong>), that <code>Option&lt;&amp;T&gt;::None</code> has the same bit pattern as <code>ptr::null()</code> follows from all of that</p>



<a name="148795344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795344">(Nov 29 2018 at 15:46)</a>:</h4>
<p>I'd been interpreting them as repr includes which bitstrings are valid just looking at the bitstring, no looking at the rest of memory.</p>



<a name="148795359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795359">(Nov 29 2018 at 15:46)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I think we need a half a page definition of these terms somewhere in the reference</p>



<a name="148795364"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795364" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795364">(Nov 29 2018 at 15:46)</a>:</h4>
<p>Validity is the invariant including the state of the rest of memory.</p>



<a name="148795422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795422">(Nov 29 2018 at 15:47)</a>:</h4>
<p>I am not sure, but I think you might be talking about <strong>safety</strong></p>



<a name="148795475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795475">(Nov 29 2018 at 15:48)</a>:</h4>
<p><strong>validity</strong> is a property of the type, it does not interact with memory beyond the type AFAICT</p>



<a name="148795515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795515">(Nov 29 2018 at 15:49)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> is this the same as your use of safety vs validity?</p>



<a name="148795533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795533">(Nov 29 2018 at 15:49)</a>:</h4>
<p><strong>safety</strong> would be, e.g., pre-conditions on unsafe methods such that safe Rust calling safe methods remains safe, or something like that</p>



<a name="148795605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795605">(Nov 29 2018 at 15:50)</a>:</h4>
<p>e.g. constructing a <code>&amp;str</code> using <code>from_utf8_unchecked</code> requires that the byte string passed as argument actually is valid UTF-8 for <strong>safety</strong></p>



<a name="148795668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795668">(Nov 29 2018 at 15:51)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> it's definitely not the same approach to defining the terms, and if they happen to coincide it's by accident. but i think the way things are going, not even that seems true -- the validity invariants mostly/likely <em>do not</em> look at other memory. (e.g. reference validity might not look at the memory the reference points at)</p>



<a name="148795671"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795671" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795671">(Nov 29 2018 at 15:51)</a>:</h4>
<p>/me is rereading <a href="https://www.ralfj.de/blog/2018/08/22/two-kinds-of-invariants.html" target="_blank" title="https://www.ralfj.de/blog/2018/08/22/two-kinds-of-invariants.html">https://www.ralfj.de/blog/2018/08/22/two-kinds-of-invariants.html</a></p>



<a name="148795685"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795685" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795685">(Nov 29 2018 at 15:51)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I think we need a half a page definition of these terms somewhere in the reference</p>
</blockquote>
<p>I was actually thinking that we merge these two discussions in the text after we are done with both</p>



<a name="148795743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795743">(Nov 29 2018 at 15:52)</a>:</h4>
<p>but yes we also need definitions^^</p>



<a name="148795750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795750">(Nov 29 2018 at 15:52)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> sorry, which two discussions?</p>



<a name="148795762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795762">(Nov 29 2018 at 15:52)</a>:</h4>
<p>having to go through the couple of blog posts to figure out all the definitions is maybe a too high a barrier of entry</p>



<a name="148795766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795766">(Nov 29 2018 at 15:52)</a>:</h4>
<p>repr and validity</p>



<a name="148795778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795778">(Nov 29 2018 at 15:53)</a>:</h4>
<p>the distinction between validity and safety for me is that validity is what must <em>always</em> hold, even in unsafe code, while safety is what safe code can rely on (but unsafe code is allowed to temporarily violate)</p>



<a name="148795822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795822">(Nov 29 2018 at 15:53)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> do we have a name for "correctness of a bitstring without looking at the rest of memory"?</p>



<a name="148795835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795835">(Nov 29 2018 at 15:53)</a>:</h4>
<p>references must always be dereferencable, but validity actually kind-of can depend on memory.</p>



<a name="148795901"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795901" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795901">(Nov 29 2018 at 15:54)</a>:</h4>
<p>(e.g. &amp;T being T-aligned, bool being just 0 and 1 etc.)</p>



<a name="148795907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795907">(Nov 29 2018 at 15:54)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> what do you mean by "correctness" ?</p>



<a name="148795915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795915">(Nov 29 2018 at 15:54)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> I don't think we have that concept. there is no situation in which a reference must be aligned but may be dangling.</p>



<a name="148795923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795923">(Nov 29 2018 at 15:54)</a>:</h4>
<p>If I have a <code>struct PrimeInt(i32);</code> would that include a bit representation that does not represent a prime number ?</p>



<a name="148795952"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795952" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795952">(Nov 29 2018 at 15:55)</a>:</h4>
<blockquote>
<p>If I have a <code>struct PrimeInt(i32);</code> would that include a bit representation that does not represent a prime number ?</p>
</blockquote>
<p>validity is compiler-defined, you cannot just pick it</p>



<a name="148795958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795958">(Nov 29 2018 at 15:55)</a>:</h4>
<p>safety OTOH can be user-defined if you have custom invariants</p>



<a name="148795968"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795968" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795968">(Nov 29 2018 at 15:55)</a>:</h4>
<p>that's why i'm asking <span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> what they mean by "correctness"</p>



<a name="148795983"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148795983" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148795983">(Nov 29 2018 at 15:55)</a>:</h4>
<p>depending on what they mean, they might be talking about validity, or safety, or both =/</p>



<a name="148796065"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796065" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796065">(Nov 29 2018 at 15:56)</a>:</h4>
<p>Good question, I guess (as with safety) there's the invariants enforced by the language, and then possibly stricter ones that are user-defined.</p>



<a name="148796093"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796093" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796093">(Nov 29 2018 at 15:57)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> it is possible to phrase things a bit differently such that validity does not depend on memory (or rather, it only depends in ways that can never be invalidated once something is valid: <code>&amp;dyn Trait</code> must have a proper vtable, that is validity, but once true it can never be non-true because vtables live in constant memory). then the fact that references are dereferencable follows from "retagging" as part of Stacked Borrows. I am not sure if that helps, though...</p>



<a name="148796185"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796185" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796185">(Nov 29 2018 at 15:58)</a>:</h4>
<p>As a concrete instance, the statement that <code>bool</code> contains only 0 and 1, is that validity?</p>



<a name="148796202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796202">(Nov 29 2018 at 15:58)</a>:</h4>
<p>so, I don't think that it is useful to have a notion of "there exists a memory for which this is valid, whether or not that's the current memory".</p>



<a name="148796205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796205">(Nov 29 2018 at 15:58)</a>:</h4>
<blockquote>
<p>As a concrete instance, the statement that <code>bool</code> contains only 0 and 1, is that validity?</p>
</blockquote>
<p>yes.</p>



<a name="148796207"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796207" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796207">(Nov 29 2018 at 15:58)</a>:</h4>
<p>Which bits a type is allowed to take without being undefined behavior is specified by <strong>validity</strong>. Which bits a type is allowed to take such that safe code remains safe is specified by <strong>safety</strong> - <code>unsafe</code> code can break <strong>safety</strong> temporarily as long as safe Rust code cannot exploit it in the meantime to run into undefined behavior - <code>unsafe</code> code cannot break <strong>validity</strong>, doing so is undefined behavior</p>



<a name="148796214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796214">(Nov 29 2018 at 15:59)</a>:</h4>
<p><code>bool</code>'s validity happens to not depend on memory</p>



<a name="148796226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796226">(Nov 29 2018 at 15:59)</a>:</h4>
<p><code>&amp;T</code>'s validity, however, <em>does</em> depend on memory</p>



<a name="148796242"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796242" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796242">(Nov 29 2018 at 15:59)</a>:</h4>
<p>you cannot create a <code>bool</code> with a value of <code>3</code> ever, not even in <code>unsafe</code> code - that's UB</p>



<a name="148796257"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796257" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796257">(Nov 29 2018 at 15:59)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> because a valid <code>&amp;T</code> is required to be dereferencable?</p>



<a name="148796329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796329">(Nov 29 2018 at 16:00)</a>:</h4>
<p>what <span class="user-mention" data-user-id="132920">@gnzlbg</span> said. moreover, both validity and safety <em>may</em> talk about memory, but they don't <em>have</em> to</p>



<a name="148796333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796333">(Nov 29 2018 at 16:00)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> because a valid <code>&amp;T</code> is required to be dereferencable?</p>
</blockquote>
<p>yes</p>



<a name="148796341"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796341" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796341">(Nov 29 2018 at 16:00)</a>:</h4>
<p>It also depends on where the object of type <code>T</code> is, if the object is at an unaligned address, then <code>&amp;T</code> bit pattern would contain an unaligned memory address, which is not <strong>valid</strong></p>



<a name="148796357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796357">(Nov 29 2018 at 16:00)</a>:</h4>
<p>because <code>&amp;T</code> is not allowed to point to unaligned memory</p>



<a name="148796376"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796376" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796376">(Nov 29 2018 at 16:00)</a>:</h4>
<p>that's something that not even <code>unsafe</code> Rust code can temporarily violate</p>



<a name="148796398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796398">(Nov 29 2018 at 16:01)</a>:</h4>
<p>Hmm, so unsafe code isn't allowed to create an undereferencable <code>&amp;T</code> temporarily?</p>



<a name="148796406"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796406" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796406">(Nov 29 2018 at 16:01)</a>:</h4>
<p>and to be clear, the alignment requirement is not really "talking about memory" -- it just inspects the address bits in the pointer</p>



<a name="148796429"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796429" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796429">(Nov 29 2018 at 16:01)</a>:</h4>
<p>right, so validity for <code>x: &amp;T</code> makes the following requirements (and maybe more, we haven't discussed that yet^^)</p>
<ul>
<li><code>x</code> is not 0x0</li>
<li><code>x</code> is aligned to <code>mem::align_of_val(x)</code></li>
<li><code>x</code> is dereferencable: <code>mem::sitze_of_val(x)</code> many bytes starting at address <code>x</code> are allocated and can be read</li>
</ul>



<a name="148796438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796438">(Nov 29 2018 at 16:01)</a>:</h4>
<p>exactly, we don't have to dereference a <code>&amp;T</code> to check whether its valid, we can always just tell by looking at <code>&amp;T</code></p>



<a name="148796519"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796519" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796519">(Nov 29 2018 at 16:02)</a>:</h4>
<p>So unsafe code can't transmute a <code>&amp;[T]</code> to a <code>&amp;T</code>?</p>



<a name="148796525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796525">(Nov 29 2018 at 16:02)</a>:</h4>
<blockquote>
<p>exactly, we don't have to dereference a <code>&amp;T</code> to check whether its valid, we can always just tell by looking at <code>&amp;T</code></p>
</blockquote>
<p>well, we have to figure out if memory is allocated. which you cannot actually do inside the language.</p>



<a name="148796532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796532">(Nov 29 2018 at 16:02)</a>:</h4>
<p>because the <code>&amp;[T]</code> might be empty.</p>



<a name="148796539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796539">(Nov 29 2018 at 16:02)</a>:</h4>
<blockquote>
<p>So unsafe code can't transmute a <code>&amp;[T]</code> to a <code>&amp;T</code>?</p>
</blockquote>
<p>correct. if the slice is empty, that's UB.</p>



<a name="148796557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796557">(Nov 29 2018 at 16:03)</a>:</h4>
<p>also they have different size so you cannot immediately transmute. I assume you mean some cast through a raw ptr.</p>



<a name="148796611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796611">(Nov 29 2018 at 16:03)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> that's a stronger requirement than C's UB IIRC.</p>



<a name="148796613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796613">(Nov 29 2018 at 16:03)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> it is</p>



<a name="148796656"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796656" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796656">(Nov 29 2018 at 16:04)</a>:</h4>
<p>it's similar to <code>int&amp;</code></p>



<a name="148796661"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796661" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796661">(Nov 29 2018 at 16:04)</a>:</h4>
<p>Note that C++ references have similar requirements</p>



<a name="148796666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796666">(Nov 29 2018 at 16:04)</a>:</h4>
<p>but even stronger</p>



<a name="148796678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796678">(Nov 29 2018 at 16:04)</a>:</h4>
<p>references just have to be dereferencable when created and when used</p>



<a name="148796689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796689">(Nov 29 2018 at 16:04)</a>:</h4>
<p>they can't be null</p>



<a name="148796691"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796691" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796691">(Nov 29 2018 at 16:04)</a>:</h4>
<p>in rust, even just assigning a variable asserts validity of the being-assigned data</p>



<a name="148796726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796726">(Nov 29 2018 at 16:05)</a>:</h4>
<p>I think that's UB in C++ too</p>



<a name="148796762"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796762" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796762">(Nov 29 2018 at 16:05)</a>:</h4>
<p>C++ references are not pointers, they are not even objects, they can't be null, they have to always point to a valid T, which due to strict aliasing, has to be properly aligned, etc. etc.</p>



<a name="148796846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796846">(Nov 29 2018 at 16:06)</a>:</h4>
<p>Is this valid C++? <code>void foo() { int *p = new int; int &amp;r = *p; delete p; }</code></p>



<a name="148796885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796885">(Nov 29 2018 at 16:06)</a>:</h4>
<p>the equivalent Rust would be UB because <code>r</code> is no longer dereferenceable at the last line</p>



<a name="148796915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148796915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148796915">(Nov 29 2018 at 16:07)</a>:</h4>
<p>it is, yes, C++ references do not have to be dereferenceable, as long as you don't dereference them obviously, they can dangle</p>



<a name="148797001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797001">(Nov 29 2018 at 16:08)</a>:</h4>
<p>so that snippet just contains a dangling reference, which is ok in C++, but this reference is not null, and its memory address is appropriately aligned for a <code>T</code></p>



<a name="148797393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797393">(Nov 29 2018 at 16:13)</a>:</h4>
<p>I always wondered why C++ compilers do not error when dangling references are returned from functions. Maybe this is why: dangling references are ok.</p>



<a name="148797458"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797458" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797458">(Nov 29 2018 at 16:14)</a>:</h4>
<p>So in Rust, the following code can produce UB<code>fn foo(x: &amp;[u8]) -&gt; Option&lt;&amp;u8&gt; { unsafe { let result = &amp;*(x as *u8); if x.len() == 0 { None } else { Some(result) } } }</code>.</p>



<a name="148797472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797472">(Nov 29 2018 at 16:14)</a>:</h4>
<blockquote>
<p>the equivalent Rust would be UB because <code>r</code> is no longer dereferenceable at the last line</p>
</blockquote>
<p>that's not true, <code>r</code> is not used again</p>



<a name="148797497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797497">(Nov 29 2018 at 16:14)</a>:</h4>
<p>i think that depends on what does <code>dereferenceable</code> mean</p>



<a name="148797563"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797563" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797563">(Nov 29 2018 at 16:16)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> if that is what you're proposing, I don't see how it justifies <code>dereferenceable</code>? Because that attribute licenses introducing loads <em>anywhere</em> in the function, even after its last use. That is the core of why Clang's use of dereferenceable is unsound, too.</p>



<a name="148797649"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797649" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797649">(Nov 29 2018 at 16:16)</a>:</h4>
<p>this would be UB though:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="p">;</span><span class="w"> </span><span class="n">drop</span><span class="p">(</span><span class="n">b</span><span class="p">);</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">r2</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">r</span><span class="p">;</span><span class="w"> </span><span class="c1">// UB: copying a dangling ref</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="148797666"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797666" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797666">(Nov 29 2018 at 16:17)</a>:</h4>
<blockquote>
<p>Because that attribute licenses introducing loads anywhere in the function, even after its last use. That is the core of why Clang's use of dereferenceable is unsound, too.</p>
</blockquote>
<p>it was my understanding that this is the case when the attribute is on a <em>parameter</em>?</p>



<a name="148797672"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797672" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797672">(Nov 29 2018 at 16:17)</a>:</h4>
<p>there must be some limit to the scope of the attribute</p>



<a name="148797686"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797686" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797686">(Nov 29 2018 at 16:17)</a>:</h4>
<p>I have no idea how it works when used inside a function. I mean, it can't just be "until the end of the current function" because then inlining would be broken</p>



<a name="148797701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797701">(Nov 29 2018 at 16:17)</a>:</h4>
<p>this would be UB as well:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="p">;</span><span class="w"> </span><span class="n">drop</span><span class="p">(</span><span class="n">b</span><span class="p">);</span><span class="w"> </span><span class="cm">/* r is dropped afterwards */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>



<a name="148797746"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797746" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797746">(Nov 29 2018 at 16:18)</a>:</h4>
<p>for parameters, Stacked Borrows has a special thing ensuring you dont allocate them while the function runs ("barriers")</p>



<a name="148797759"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797759" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797759">(Nov 29 2018 at 16:18)</a>:</h4>
<blockquote>
<p>this would be UB as well:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">b</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">Box</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="o">*</span><span class="n">b</span><span class="p">;</span><span class="w"> </span><span class="n">drop</span><span class="p">(</span><span class="n">b</span><span class="p">);</span><span class="w"> </span><span class="cm">/* r is dropped afterwards */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


</blockquote>
<p>no, <code>r</code> has no drop glue</p>



<a name="148797776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797776">(Nov 29 2018 at 16:18)</a>:</h4>
<p>and NLL lets us witness that that is the case</p>



<a name="148797784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797784">(Nov 29 2018 at 16:18)</a>:</h4>
<p>but it is still alive, even if there is nothing to drop ?</p>



<a name="148797793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797793">(Nov 29 2018 at 16:18)</a>:</h4>
<p>or is <code>r</code> "dead" before <code>drop(b)</code> ?</p>



<a name="148797796"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797796" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797796">(Nov 29 2018 at 16:18)</a>:</h4>
<p>it does not require <code>r</code> to be live at the end</p>



<a name="148797808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797808">(Nov 29 2018 at 16:18)</a>:</h4>
<blockquote>
<p>or is <code>r</code> "dead" before <code>drop(b)</code> ?</p>
</blockquote>
<p>yes</p>



<a name="148797825"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797825" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797825">(Nov 29 2018 at 16:19)</a>:</h4>
<p>so values of types without drop glue are dead "as soon as possible" ?</p>



<a name="148797844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797844">(Nov 29 2018 at 16:19)</a>:</h4>
<blockquote>
<p>for parameters, Stacked Borrows has a special thing ensuring you dont allocate them while the function runs ("barriers")</p>
</blockquote>
<p>OK I've not kept up with stacked borrows as much as I wanted to so I'm going to take this on faith and then it should be fine. I was just confused because it sounded a bit like the validity invariant "points to allocated memory of right size" was supposed to justify the attribute on its own.</p>



<a name="148797854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797854">(Nov 29 2018 at 16:19)</a>:</h4>
<p>no. it's the other way around: values of types with drop glue have a use when they are dropped</p>



<a name="148797919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797919">(Nov 29 2018 at 16:20)</a>:</h4>
<p>so values of types without drop glue die on their last use ?</p>



<a name="148797924"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797924" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797924">(Nov 29 2018 at 16:20)</a>:</h4>
<p>and then yes stuff with function scope is complicated, and that's where we cannot really tease apart validity and Stacked Borrows any more. (or, we can, but now "dereferencable" becomes part of stacked borrows, not of validity.)</p>



<a name="148797936"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797936" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797936">(Nov 29 2018 at 16:21)</a>:</h4>
<p>not sure what you mean by "die"</p>



<a name="148797945"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797945" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797945">(Nov 29 2018 at 16:21)</a>:</h4>
<p>they have to be valid at all uses</p>



<a name="148797958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797958">(Nov 29 2018 at 16:21)</a>:</h4>
<blockquote>
<p>so values of types without drop glue die on their last use ?</p>
</blockquote>
<p>Values don't die, they cease to be live because there are no more uses. The reason types with drop glue <em>don't</em> "die early" is because the drop is a use very late in the function.</p>



<a name="148797966"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148797966" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148797966">(Nov 29 2018 at 16:21)</a>:</h4>
<p>end of story. "dereferencable" is a bit special in that it is entangled with Stacked Borrows because of its function scope</p>



<a name="148798053"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148798053" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148798053">(Nov 29 2018 at 16:22)</a>:</h4>
<p>TIL - thanks - I think I have to read about "valid at all uses", that wasn't really part of my mental model</p>



<a name="148799188"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799188" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799188">(Nov 29 2018 at 16:40)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> it's not really cyclic - it's more like a dag</p>



<a name="148799315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799315">(Nov 29 2018 at 16:42)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> it's mutually recursive but well-founded, as repr of an enum only depends on validity of its fields.</p>



<a name="148799319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799319">(Nov 29 2018 at 16:42)</a>:</h4>
<p>for any given type T, validity depends on the representation of T, and the validity and representation of all T's sub-objects</p>



<a name="148799329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799329">(Nov 29 2018 at 16:43)</a>:</h4>
<p>mmh</p>



<a name="148799334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799334">(Nov 29 2018 at 16:43)</a>:</h4>
<p>yeah, okay</p>



<a name="148799357"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799357" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799357">(Nov 29 2018 at 16:43)</a>:</h4>
<p>I think you said the same thing in different words^^</p>



<a name="148799363"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799363" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799363">(Nov 29 2018 at 16:43)</a>:</h4>
<p>yep!</p>



<a name="148799384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799384">(Nov 29 2018 at 16:43)</a>:</h4>
<p>"well-founded" is a good term</p>



<a name="148799448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799448">(Nov 29 2018 at 16:44)</a>:</h4>
<p>sometimes, research is good for something ;)</p>



<a name="148799523"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799523" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799523">(Nov 29 2018 at 16:45)</a>:</h4>
<p>I am literally a mathematics major :P</p>



<a name="148799529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799529">(Nov 29 2018 at 16:45)</a>:</h4>
<p>I know, I'm joking :D</p>



<a name="148799632"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799632" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799632">(Nov 29 2018 at 16:47)</a>:</h4>
<p>I'm taking an abstract algebra course next quarter</p>



<a name="148799637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799637">(Nov 29 2018 at 16:47)</a>:</h4>
<p>I am _very_ excited</p>



<a name="148799753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148799753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148799753">(Nov 29 2018 at 16:49)</a>:</h4>
<p><span class="emoji emoji-2764" title="heart">:heart:</span> algebra</p>



<a name="148801099"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/148801099" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#148801099">(Nov 29 2018 at 17:07)</a>:</h4>
<p>Submitted <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/51" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/51">https://github.com/rust-rfcs/unsafe-code-guidelines/pull/51</a></p>



<a name="151396609"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151396609" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151396609">(Dec 10 2018 at 20:03)</a>:</h4>
<p>OK, silly naming question. Does "reference" in Rust include trait objects or not?</p>



<a name="151396751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151396751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151396751">(Dec 10 2018 at 20:04)</a>:</h4>
<p>I think the answer is "yes", in which case how do I refer to "<code>&amp;T</code> or <code>&amp;mut T</code> where <code>T</code> is a type"?</p>



<a name="151396953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151396953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151396953">(Dec 10 2018 at 20:06)</a>:</h4>
<p>This is coming up in the context of <span class="user-mention" data-user-id="124289">@rkruppe</span>'s comment at <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/51#pullrequestreview-181964318" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/51#pullrequestreview-181964318">https://github.com/rust-rfcs/unsafe-code-guidelines/pull/51#pullrequestreview-181964318</a></p>



<a name="151397208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151397208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151397208">(Dec 10 2018 at 20:08)</a>:</h4>
<p>for which the fix is to say something like "The alignment of <code>&amp;T</code> and <code>&amp;mut T</code>, where <code>T</code> is a type, is the word size" which is quite clunky.</p>



<a name="151397368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151397368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> QuietMisdreavus <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151397368">(Dec 10 2018 at 20:09)</a>:</h4>
<p>in code you'd say <code>where T: Sized</code>, iirc, but i'm not totally sure how to word that for docs that succinctly</p>



<a name="151397618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151397618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151397618">(Dec 10 2018 at 20:11)</a>:</h4>
<p><span class="user-mention" data-user-id="133692">@QuietMisdreavus</span> trait objects are the issue here, the problem is that the alignment of <code>&amp;dyn Trait</code>might be more than the word size, sigh.</p>



<a name="151397822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151397822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151397822">(Dec 10 2018 at 20:13)</a>:</h4>
<p>oh, you're right, custom DSTs too.</p>



<a name="151404477"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151404477" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151404477">(Dec 10 2018 at 21:56)</a>:</h4>
<p><span class="user-mention" data-user-id="128323">@Alan Jeffrey</span> I am a bit confused, because your two first questions do not connect.^^ yes, reference include <code>&amp;dyn Trait</code>. in <code>&amp;T</code>, <code>T</code> is always a type. we have some old syntax where without context we can confuse types and traits, but we also have <code>dyn Trait</code> as new syntax to avoid that ambiguity.</p>



<a name="151404558"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151404558" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151404558">(Dec 10 2018 at 21:58)</a>:</h4>
<p>also when you are talking about the alignment of <code>&amp;T</code>, do you mean the alignment of this type itself (the same way that <code>i32</code> has alignment 4), or the required alignment of <em>values of the given type</em> (which is just the alignment of <code>T</code>)?</p>



<a name="151406454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151406454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151406454">(Dec 10 2018 at 22:26)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I was meaning the alignment of <code>&amp;T</code> itself, not the valid values (which are multiples of <code>T</code>s alignment).</p>



<a name="151406500"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151406500" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151406500">(Dec 10 2018 at 22:27)</a>:</h4>
<p>The problem being that if <code>T</code> is a custom DST, then <code>&amp;T</code> might have alignment greater than the word, sigh.</p>



<a name="151406568"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151406568" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151406568">(Dec 10 2018 at 22:28)</a>:</h4>
<p>So in order to not box ourselves in for DSTs, we need to allow <code>&amp;T</code> to have alignment that's possibly greater than a word.</p>



<a name="151406576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151406576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151406576">(Dec 10 2018 at 22:28)</a>:</h4>
<p>Sigh.</p>



<a name="151428988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151428988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151428988">(Dec 11 2018 at 07:50)</a>:</h4>
<p>ah I see</p>



<a name="151428989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151428989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151428989">(Dec 11 2018 at 07:50)</a>:</h4>
<p>I don't see what's so bad about that though^^ you can always determine the current alignment with <code>align_of</code>, after all</p>



<a name="151453915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151453915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alan Jeffrey <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151453915">(Dec 11 2018 at 15:31)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> it makes the definition wordier, rather than just "<code>&amp;T</code> is word-aligned" we have "<code>&amp;T</code> is at least word-aligned, and is word-aligned in the following cases..."</p>



<a name="151453969"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151453969" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151453969">(Dec 11 2018 at 15:32)</a>:</h4>
<p>seems like the definition should be in terms of a desugaring to a <code>struct</code></p>



<a name="151454054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151454054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151454054">(Dec 11 2018 at 15:32)</a>:</h4>
<p>there are no new rules here, right? the fact that <code>u64</code> metadata can increase alignment is just like <code>(*const (), u64)</code> having a larger-than-word alignment</p>



<a name="151454687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation%20of%20references/near/151454687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/representation.20of.20references.html#151454687">(Dec 11 2018 at 15:41)</a>:</h4>
<p>that's why I preferred the generic definition for DSTs, but the equivalent without that is to just say that "<code>&amp;T</code> has the same layout as ..."</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>